/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.farent.solutions.plugins.aribaweb;

import java.awt.Component;
import java.io.File;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.LinkedHashSet;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.regex.Matcher;
import javax.swing.JComponent;
import javax.swing.event.ChangeListener;
import org.netbeans.spi.project.support.ant.AntProjectHelper;
import org.netbeans.spi.project.ui.support.ProjectChooser;
import org.openide.WizardDescriptor;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.util.NbBundle;

/**
 *
 * @author farouka
 */
public class NBawProjectWizardIterator  implements WizardDescriptor./*Progress*/InstantiatingIterator {

    private int index;
    private WizardDescriptor.Panel[] panels;
    private WizardDescriptor wiz;
    
    public NBawProjectWizardIterator()
    {
    }

    public static NBawProjectWizardIterator createIterator()
    {
        return new NBawProjectWizardIterator();
    }

    private WizardDescriptor.Panel[] createPanels()
    {
        return new WizardDescriptor.Panel[]
                {
                    new NBawProjectWizardPanel()
                };
    }

    private String[] createSteps()
    {
        return new String[]
                {
                    NbBundle.getMessage( NBawProjectWizardIterator.class, "LBL_CreateProjectStep" )
                };
    }

    public Set instantiate() throws IOException
    {
        Set<FileObject> resultSet = new LinkedHashSet<FileObject>();
        File dirF = (File)wiz.getProperty("projdir");        //NOI18N
        if (dirF != null) {
            dirF = FileUtil.normalizeFile(dirF);
        }
        String name = (String)wiz.getProperty("name");

        String awHome = (String)wiz.getProperty( NBawConstants.AW_HOME );
//        if (awHome != null) {
//            if (!awHome.endsWith(File.separator)) {
//                awHome += File.separatorChar;
//            }
//        }

        AntProjectHelper h = NBawProjectGenerator.createProject( dirF, name, awHome );

        File parent = dirF.getParentFile();
        if ( parent != null && parent.exists() )
        {
            ProjectChooser.setProjectsFolder( parent );
        }
        FileObject dir = FileUtil.toFileObject(dirF);
        resultSet.add (dir);

        return resultSet;

    }

    public void initialize( WizardDescriptor wiz )
    {
        this.wiz = wiz;
        index = 0;
        panels = createPanels();
        // Make sure list of steps is accurate.
        String[] steps = createSteps();
        for ( int i = 0; i < panels.length; i++ )
        {
            Component c = panels[i].getComponent();
            if ( steps[i] == null )
            {
                // Default step name to component name of panel.
                // Mainly useful for getting the name of the target
                // chooser to appear in the list of steps.
                steps[i] = c.getName();
            }
            if ( c instanceof JComponent )
            { // assume Swing components
                JComponent jc = ( JComponent ) c;
                // Step #.
                jc.putClientProperty( "WizardPanel_contentSelectedIndex", new Integer( i ) );
                // Step name (actually the whole list for reference).
                jc.putClientProperty( "WizardPanel_contentData", steps );
            }
        }
    }

    public void uninitialize( WizardDescriptor wiz )
    {
        this.wiz.putProperty( "projdir", null );
        this.wiz.putProperty( "name", null );
        this.wiz = null;
        panels = null;
    }

    public String name()
    {
        return MessageFormat.format( "{0} of {1}",
                new Object[]
                {
                    new Integer( index + 1 ), new Integer( panels.length )
                } );
    }

    public boolean hasNext()
    {
        return index < panels.length - 1;
    }

    public boolean hasPrevious()
    {
        return index > 0;
    }

    public void nextPanel()
    {
        if ( !hasNext() )
        {
            throw new NoSuchElementException();
        }
        index++;
    }

    public void previousPanel()
    {
        if ( !hasPrevious() )
        {
            throw new NoSuchElementException();
        }
        index--;
    }

    public WizardDescriptor.Panel current()
    {
        return panels[index];
    }

    // If nothing unusual changes in the middle of the wizard, simply:
    public final void addChangeListener( ChangeListener l )
    {
    }

    public final void removeChangeListener( ChangeListener l )
    {
    }
}
